/* 
 * File:   Network.cpp
 * Author: Martin
 * 
 * Created on 29. červen 2010, 0:14
 */

#include "Network.h"

Network::Network(int INPUT_SIZE) {
    this->INPUT_SIZE = INPUT_SIZE;
    //TODO: dynamicka alokace neuronu a input_buffer
    init();
}

/**
 * Loads network from file containing stored neurons with their weights
 * //EXTRA: and also INPUT_SIZE, NETWORK_SIZE
 */
Network::Network(string in_file) {
    //TODO
}

Network::Network(const Network& orig) {
}

Network::~Network() {
    in_file.close();
}

void Network::init() {
    srand((unsigned) time(0));
    //Neuron& neuron;
    //neurons2 = (Neuron **) malloc(sizeof (double) * INPUT_SIZE2);
    for (int i = 0; i < NETWORK_SIZE; i++) {
        //neurons2[i] = (Neuron *) malloc(sizeof (double) * INPUT_SIZE2);
        for (int j = 0; j < NETWORK_SIZE; j++) {
            Neuron neuron = Neuron(INPUT_SIZE);
            //link& = neuron;
            neurons[i][j] = neuron;
            //neurons2[i][j] = neuron;
        }
    }
    input_vector = (double **) malloc(sizeof (double) * INPUT_SIZE);
    for (int i = 0; i < INPUT_SIZE; i++) {
        input_vector[i] = (double *) malloc(sizeof (double) * INPUT_SIZE);
    }

}

int Network::assign_data_file(std::string in_file) {
    Network::in_file.open(in_file.data());

    if (!Network::in_file) {
        cerr << "Couldn't open input file '" << in_file << "'" << endl;
        return 1;
    }
}

int Network::read_next_input() {
    if (in_file.eof()) {
        cerr << "At the end of data file" << endl;
        return 1;
    }
    string buffer_str;
    getline(in_file, buffer_str);
    char* token;
    char* buffer_char = new char[strlen(buffer_str.c_str())];
    strcpy(buffer_char, buffer_str.c_str());
    token = strtok(buffer_char, ",");
    double double_val;
    int i = 0;
    while (token != NULL) {
        double_val = atof(token);
        input_vector[i] = double_val;
        token = strtok(NULL, ",");
        i++;
    }
    return 0;
}

int Network::reset_data_file() {
    in_file.clear();
    in_file.seekg(0);
    if (in_file.tellg() == 0) {
        return 0;
    } else {
        return 1;
    }
}

int Network::get_position_in_file(bool print) {
    int position = in_file.tellg();
    if (print) {
        cout << "position in file: " << position << endl;
    }
    return position;
}

bool Network::end_of_input() {
    return in_file.eof();
}

int Network::get_input_size(){
    return INPUT_SIZE;
}

void Network::print() {
    for (int i = 0; i < NETWORK_SIZE; i++) {
        for (int j = 0; j < NETWORK_SIZE; j++) {
            std::cout << "Neuron [" << i << "][" << j << "]: " << std::endl;
            neurons[i][j].print();
            //neurons2[i][j].print();
        }
    }
}

void Network::print_current_input() {
    cout << "Current input: " << endl;
    for (int i = 0; i < INPUT_SIZE; i++) {
        cout << input_vector[i] << ",";
    }
    cout << endl;
}


